# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=xpdf
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.05
pkgrel=1
pkgdesc="Utilities for PDF files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.xpdfreader.com/"
msys2_references=(
  "cpe: cpe:/a:xpdf:xpdf"
  "cpe: cpe:/a:xpdf_project:xpdf"
  "cpe: cpe:/a:xpdfreader:xpdf"
)
license=('spdx:GPL-2.0-only OR GPL-3.0-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libpaper"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-qt6-base")
source=(https://dl.xpdfreader.com/xpdf-${pkgver}.tar.gz{,.sig}
        002-xpdf-4.00-64bit.patch
        005-xpdf-4.00-permissions.patch)
sha256sums=('92707ed5acb6584fbd73f34091fda91365654ded1f31ba72f0970022cf2a5cea'
            'SKIP'
            '75f5d7ae4bb83d2c380dfc9f65bad78574ac3f038fdd6be73c29fc9ea817e715'
            '22e0afda66f036cea8643252762e1391ed078102dd5f76f8cf862976b4b37089')
validpgpkeys=('A56006CA75CF8B13FA2F120DF4825F5397271342')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    002-xpdf-4.00-64bit.patch \
    005-xpdf-4.00-permissions.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DA4_PAPER=ON \
    -DMULTITHREADED=ON \
    -DOPI_SUPPORT=ON \
    -DSPLASH_CMYK=ON \
    -DXPDFWIDGET_PRINTING=ON \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  # Copy xpdf-icon.ico to build directory to fix building with GCC
  cp ../${_realname}-${pkgver}/xpdf-qt/xpdf-icon.ico .

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}"/${_realname}-${pkgver}
  install -Dm 644 xpdf-qt/xpdf-icon.svg "${pkgdir}${MINGW_PREFIX}/share/pixmaps/xpdf.svg"
  install -Dm 644 README CHANGES -t "${pkgdir}${MINGW_PREFIX}/share/doc/${pkgname}"

  # remove stuff provided by poppler
  for tool in pdfdetach pdffonts pdfimages pdfinfo pdftoppm pdftops pdftotext pdftohtml; do
    rm "${pkgdir}${MINGW_PREFIX}/bin/${tool}" "${pkgdir}${MINGW_PREFIX}/share/man/man1/${tool}.1"
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING3 "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3
}
